<?php

function amitavroy_site_block_linkify_twitter_status($status_text) {
  // linkify URLs
  $status_text = preg_replace(
    '/(https?:\/\/\S+)/',
    '<a href="\1" class="preg-links">\1</a>',
    $status_text
  );
 
  // linkify twitter users
  $status_text = preg_replace(
    '/(^|\s)@(\w+)/',
    '\1<a href="http://twitter.com/\2" class="preg-links">@\2</a>',
    $status_text
  );
 
  // linkify tags
  $status_text = preg_replace(
    '/(^|\s)#(\w+)/',
    '\1#<a href="http://search.twitter.com/search?q=%23\2" class="preg-links">\2</a>',
    $status_text
  );
 
  return $status_text;
}

function isWeatherReport($text) {
	$pattern = "/Sol\s\d+\s\([\w\W]+\):/";
	return preg_match($pattern, $text);
}
//$data = 'http://search.twitter.com/search.json?q=from:MarsWxReport&count=12'; 
$data = 
'http://api.twitter.com/1/statuses/user_timeline.json?'.
'&include_entities=true'.
'&include_rts=true'.
'&screen_name=MarsWxReport'.
//'&screen_name=yordandar'.
'&count=100'.
// '&max_id=310005506813075456'.
// '&since_id=306890226452799489'.
'';
$feed = file_get_contents($data); //Getting the JSON data.
//print_r($feed);
$valid_data = json_decode($feed); // Converting the JSON data to PHP format.
//$valid_data = $valid_data->results; // Valid data now with just the tweet result.
 
// Printing out the feed's data in our required format.
print '<pre>';
foreach ($valid_data as $key=>$value) {
	
	if (isWeatherReport($value->text) != 1) {
		
		if (isset($value->retweeted_status)) {
		  	//print_r($value);
		  	print("Retwiiiittttteeeedddd");
		  	print '<div id="twitter-data-container">';
			print('<p><strong>'.$value->id_str.'</strong></p>');
			print '<img src="' . $value->retweeted_status->user->profile_image_url . '" />';
			print '<p>' . amitavroy_site_block_linkify_twitter_status($value->text) . '</p>';
			print '</div>';
		}
		else {
			  //print_r($value);
			  print '<div id="twitter-data-container">';
			  print('<p><strong>'.$value->id_str.'</strong></p>');
			  print '<img src="' . $value->user->profile_image_url . '" />';
			  print '<p>' . amitavroy_site_block_linkify_twitter_status($value->text) . '</p>';
			  print '</div>';
		}
    }
}
print '</pre>';
?>